﻿using System;
using Besture.Bll;
using Besture.Common;
using Besture.Common.Helper;
using Besture.Model;
using Besture.UI.Admin.Master;


namespace Besture.UI.Admin.CommonModel
{
    public partial class FieldEdit : BasePage
    {

        public string _ModelID = string.Empty;

        public string _ID = string.Empty;

        public string _FieldType = "1";

        public string _OptionType = "1";


        #region 页面加载事件

        /// <summary>
        /// 页面加载
        /// </summary>
        protected override void InitialLoad()
        {
            _ModelID = Query("ModelID");

            _ID = Query("ID");

            ViewState["ModelID"] = _ModelID;

            ViewState["ID"] = _ID;
            //页面加载
            InitPage();
            //编辑编号
            InitData(ID);

            ((ConfigAdmin)this.Master).Title = "内容模型管理";
        }

        /// <summary>
        /// 回发
        /// </summary>
        protected override void PostBackLoad()
        {

        }

        /// <summary>
        /// 页面加载
        /// </summary>
        public void InitPage()
        {

        }

        /// <summary>
        /// 数据加载
        /// </summary>
        /// <param name="id">编号</param>
        protected void InitData(int id)
        {
            var dac = new ContentModelFieldDac();
            var model = dac.GetModel(ID);

            //编辑时禁止修改字段名称
            txtName.Enabled = false;

            txtName.Text = model.Name;
            txtAlias.Text = model.Alias;
            txtTips.Text = model.Tips;
            _FieldType = model.FieldType.ToString();
            txtBoxLength.Text = model.TextBoxWidth.ToString();
            ddlValidate.SelectedValue = model.ValidateRule;
            txtDefaultValue.Text = model.DefaultValue;
            txtDisWidth.Text = model.DisWidth.ToString();
            txtDisHeight.Text = model.DisHeight.ToString();
            _OptionType = model.OptionType.ToString();
            txtPhotoType.Text = model.PhotoType;
            txtPhotoPath.Text = model.PhotoPath;
            txtFileType.Text = model.FileType;
            txtFilePath.Text = model.FilePath;
            txtOptionValue.Text = model.OptionValue;
            cbDefaultTime.Checked = Convert.ToBoolean(model.IsDefaultTime);
            ddlListDisplay.SelectedValue = model.ListDisplay.ToString();

        }
        #endregion

        #region 保存

        protected void btnSubmit_Click(object sender, EventArgs eventArgs)
        {
            var dac = new ContentModelFieldDac();
            ContentModelFieldModel model = dac.GetModel(ID);

            model.ModelID = Convert.ToInt32(ViewState["ModelID"]);
            model.Name = txtName.Text;
            model.Alias = txtAlias.Text;
            model.Tips = txtTips.Text;
            
            model.MaxLength = Convert.ToInt32(txtMaxLength.Text);
            model.TextBoxWidth = Convert.ToInt32(txtBoxLength.Text);
            model.ValidateRule = ddlValidate.SelectedValue;
            model.DefaultValue = txtDefaultValue.Text;
            model.DisWidth = Convert.ToInt32(txtDisWidth.Text);
            model.DisHeight = Convert.ToInt32(txtDisHeight.Text);
            model.OptionValue = txtOptionValue.Text;
            model.OptionType = Convert.ToInt32(Request.Form["rdOptionDisType"]);
            model.PhotoType = txtPhotoType.Text;
            model.PhotoPath = txtPhotoPath.Text;
            model.FileType = txtFileType.Text;
            model.FilePath = txtFilePath.Text;
            model.IsDefaultTime = Convert.ToInt32(cbDefaultTime.Checked);
            model.ListDisplay = Convert.ToInt32(ddlListDisplay.SelectedValue);
            model.Required = Convert.ToInt32(cbRequired.Checked);
 
            dac.Update(model);

            FunHelper.Alert("保存成功", "FieldList.aspx?ModelID=" + model.ModelID);

        }
        #endregion

        #region 取消

        public void btnCanel_Click(object sender, EventArgs eventArgs)
        {

        }

        #endregion
    }
}